<html>
    <head>
<title> Have a nice day </title>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
require_once 'model/db.php';
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 

    </head>
 <body>
<?php 
    $query = "SELECT * FROM companies";
    $companies = getData($query);
?>
<?php 
    $query = "SELECT * FROM apps";
    $apps = getData($query);
 ?>
<h1><i>Have a nice day</i></h1>
<form method="POST">
  <pre>
    Min fans <input type="text" name="min_fans" value ="1" id ="min_fans"><br>
    Max fans <input type="text" name="max_fans" value ="10"><br>
    Keyword  <input type="text" name="keyword" value ="cafe"><br>
    Company  <select name="id_company">
    <option value="0"> please choose </option>
        <?php foreach ($companies as $company): ?>
        <option value="<?php echo $company['id'] ?>"><?php echo $company['name']?></option>
        <?php        endforeach;?>
   </pre>
    </select>
      App    <select name="id_app">
         <option value="0"> please choose </option>
        <?php foreach ($apps as $app): ?>
        <option value="<?php echo $app['pid'] ?>"><?php echo $app['name']?></option>
        <?php        endforeach;?>
    </select>
    <input type="submit" name="submit">
    <input type="button" name="check" value ="check" id="check">
</form>

<?php if (isset($_POST['submit'])):?>
    <?php 
    
        $min_fans = $_POST['min_fans'];//TODO
        $max_fans = $_POST['max_fans'];//TODO
        $keyword = $_POST['keyword'];//TODO
        $company=$_POST["id_company"];
        $apps=$_POST["id_app"];
        if(!empty($company))
            $company="companies.id=$company AND";
        else
           $company="";
        if(!empty($apps))
            $apps="apps.id='$apps' AND";
        else
           $apps="";
        if(empty($max_fans)&& !empty($min_fans))
        {
         $query = "SELECT * FROM pages 
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                  WHERE 
                      (likes >= $min_fans)
                      AND (description LIKE '% $keyword %')
                  LIMIT 0, 10
                  ";   
        }
        else if(!empty($max_fans)&& empty($min_fans))
        {
         $query = "SELECT * FROM pages 
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                 WHERE $company
                       $apps 
                       (likes <= $max_fans)
                       AND (description LIKE '% $keyword %')
                      
                  LIMIT 0, 10
                  ";   
        }
        else if(empty($max_fans)&& empty($min_fans))
        {
         $query = "SELECT * FROM pages
            INNER JOIN apps_pages ON pages.id=apps_pages.id_page
            INNER JOIN apps ON apps.id=apps_pages.id_app
            INNER JOIN companies ON companies.id=apps.id_company
                  WHERE 
                    $company
                    $apps 
                    (description LIKE '% $keyword %')
                     LIMIT 0, 10 ";   
        }
        else
        {  $query = "SELECT * FROM pages    
                 
                  WHERE
                    $company
                    $apps 
                    (likes >= $min_fans)
                     AND (likes <= $max_fans)
                     AND (description LIKE '% $keyword %')
                     LIMIT 0, 10 " ;       
        }
       var_dump($query);
        $pages = getData($query);
    ?>
    <?php if (!empty($pages)):?>
    <table border='1'>
        <tr>
            <td> profile </td>
            <td> likes </td>
            <td> Company</td>
        </tr>
        <?php foreach ($pages as $page):?>
            <tr>
                <td><img src="<?php echo $page['picture']?>"></td>
                <td><?php echo $page['likes'] ?></td>
                <td><?php echo $page['name'] ?></td>   
        <?php ?>
            </tr>
        <?php endforeach; ?>
    </table> 
    <?php endif;?> 
<?php endif;?>

<script>
    $(document).ready(function(){
        $('#check').click(function(){
            var min_fans = $('min_fans').val();
            var flag = checkNumber(min_fans);
            if(flag){
                alert('Is number');
            }else
                alert('Is not number');
        });
        function checkNumber(val){
            if($.isNumeric(val)){
                return true;
            }else{
                return false;
        }
        }
    })

</script>
        </body>
</html>
